package cn.qingyun.gis.modules.baogan.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.qingyun.gis.modules.baogan.entity.CrmStaff;
import cn.qingyun.gis.modules.baogan.entity.MicrogridCareRecordEntity;
import cn.qingyun.gis.modules.baogan.mapper.BaoganTeamMapper;
import cn.qingyun.gis.modules.baogan.mapper.CrmStaffMapper;
import cn.qingyun.gis.modules.baogan.mapper.MicrogridCareRecordMapper;
import cn.qingyun.gis.modules.baogan.service.BaoganTeamService;
import cn.qingyun.gis.modules.baogan.service.CrmStaffService;
import cn.qingyun.gis.modules.baogan.vo.request.GridCodeAndKeyListRequest;
import cn.qingyun.gis.modules.baogan.vo.request.StaffRequest;
import cn.qingyun.gis.modules.baogan.vo.response.StaffAndTeamVo;
import cn.qingyun.gis.modules.baogan.vo.response.StaffItemVo;
import cn.qingyun.gis.modules.baogan.vo.response.TeamItemVo;
import cn.qingyun.gis.modules.baogan.vo.response.TransferResponse;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * CRM人员 服务实现类
 * </p>
 *
 * @author guest
 * @since 2024-01-04
 */
@Service
@Slf4j
@DS("scenes_offensive")
public class CrmStaffServiceImpl extends ServiceImpl<CrmStaffMapper, CrmStaff> implements CrmStaffService {

    @Autowired
    private BaoganTeamService baoganTeamService;
    @Autowired
    private BaoganTeamMapper baoganTeamMapper;
    @Resource
    private MicrogridCareRecordMapper careRecordMapper;

    /**
     * 看管划分-查询自有人员及团队
     *
     * @return
     */
    @Override
    public StaffAndTeamVo listStaffAndTeam(GridCodeAndKeyListRequest request) {
        StaffAndTeamVo staffAndTeamVo = new StaffAndTeamVo();

        //查询人员
        LambdaQueryWrapper<CrmStaff> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(CrmStaff::getGridCode, request.getGridCode())
                .eq(CrmStaff::getSelfFlag, "是")
                .and(StringUtil.isNotBlank(request.getKey()),qw -> qw.like(CrmStaff::getName, request.getKey()).or().like(CrmStaff::getCrmAccount, request.getKey()));
        List<CrmStaff> staffList = this.list(queryWrapper);
        if (CollUtil.isNotEmpty(staffList)) {
            List<StaffItemVo> staffItemVoList = staffList.stream().map(item -> {
                StaffItemVo staffItemVo = new StaffItemVo();
                BeanUtils.copyProperties(item, staffItemVo);
                return staffItemVo;
            }).collect(Collectors.toList());
            staffAndTeamVo.setStaffList(staffItemVoList);
        }

        //查询团队
        List<TeamItemVo> TeamList = baoganTeamMapper.listTeamByGridAndKey(request.getGridCode(),request.getKey());
        staffAndTeamVo.setTeamList(TeamList);

        return staffAndTeamVo;
    }
}




